package com.prs.crm.action.oa;

import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.FileSend;
import com.prs.crm.domain.oa.FileSendAccept;
import com.prs.crm.domain.oa.FileSendCollect;
import com.prs.crm.domain.oa.Notice;
import com.prs.crm.domain.oa.NoticeCollect;
import com.prs.crm.service.oa.FileSendAcceptService;
import com.prs.crm.service.oa.FileSendCollectService;
import com.prs.crm.service.oa.FileSendService;

public class FileSendAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private FileSend fileSend;
	private FileSendService fileSendService;
	private FileSendAcceptService fileSendAcceptService;
	private FileSendCollectService fileSendCollectService;
	private Integer[] ids;
	private String accepts;
	private File upload;
	private String uploadFileName;
	
	@Action("editFileSend")
	public String editFileSend(){
		return SUCCESS;
	}
	
	@Action("listFileSend")
	public String listFileSend(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				FileSend.class);
		criteria.add(Restrictions.eq("oabox", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFileSendService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("listCaogaoFile")
	public String listCaogaoFile(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				FileSend.class);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("recorder", person));
		criteria.add(Restrictions.eq("oabox", 0));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFileSendService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action(value = "insertFileSendToCollect", results = {
			@Result(name = "success", type = "redirect", location = "listFileSend"),
			@Result(name = "error", type = "chain", location = "listFileSend") })
	public String insertFileSendToCollect() {
		if (ids == null) {
			this.addActionError("请选择要收藏的文件");
			return ERROR;
		}
		for (Integer id : ids) {
			FileSend fileSend = this.getFileSendService().get(id);
			FileSendCollect collect = new FileSendCollect();
			collect.setFileSend(fileSend);
			this.getFileSendCollectService().save(collect);
		}
		return SUCCESS;
	}

	@Action(value = "removeCaogaoFile", results = {
			@Result(name = "success", type = "redirect", location = "listCaogaoFile"),
			@Result(name = "error", type = "chain", location = "listCaogaoFile") })
	public String removeCaogaoFile(){
		if(ids == null){
			this.addActionError("请选择要删除的草稿文件");
			return ERROR;
		}
		this.getFileSendService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Clew(results = { "success" })
	@Action(value = "saveFileSend", results = {
			@Result(name = "success", type = "redirect", location = "editFileSend"),
			@Result(name = "error", type = "chain", location = "editFileSend") })
	public String saveFileSend() {
		if (fileSend.getTitle() == null || "".equals(fileSend.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		if (accepts == null || "".equals(accepts.trim())) {
			addActionError("收件人不能为空");
			return ERROR;
		}
		this.setPrompt("文件发送成功");
		fileSend.setOabox(1);
		this.getFileSendService().saveFileSend(fileSend, upload, uploadFileName,
				accepts);
		return SUCCESS;
	}
	
	@Clew(results = { "success" })
	@Action(value = "saveFileSend2", results = {
			@Result(name = "success", type = "redirect", location = "listCaogaoFile"),
			@Result(name = "error", type = "chain", location = "editCaogaoFile") })
	public String saveFileSend2() {
		if (fileSend.getTitle() == null || "".equals(fileSend.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		if (accepts == null || "".equals(accepts.trim())) {
			addActionError("收件人不能为空");
			return ERROR;
		}
		this.setPrompt("文件发送成功");
		fileSend.setOabox(1);
		this.getFileSendService().saveFileSend(fileSend, upload, uploadFileName,
				accepts);
		return SUCCESS;
	}
	
	@Clew(results = { "success" })
	@Action(value = "saveFileSendToCaogao", results = {
			@Result(name = "success", type = "redirect", location = "editFileSend"),
			@Result(name = "error", type = "chain", location = "editFileSend") })
	public String saveFileSendToCaogao() {
		if (fileSend.getTitle() == null || "".equals(fileSend.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		this.setPrompt("文件保存到草稿箱成功");
		fileSend.setOabox(0);
		this.getFileSendService().saveFileSendToCaogao(fileSend, upload, uploadFileName);
		return SUCCESS;
	}
	
	@Action("viewFileSend")
	public String viewFileSend(){
		return SUCCESS;
	}
	
	@Action(value = "removeFileSend", results = {
			@Result(name = "success", type = "redirect", location = "listFileSend"),
			@Result(name = "error", type = "chain", location = "listFileSend") })
	public String removeFileSend() {
		if (ids == null) {
			this.addActionError("请选择要删除的文件");
			return ERROR;
		}
		this.getFileSendService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Action("listReceiveFile")
	public String listReceiveFile(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				FileSendAccept.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("person", person));
		this.setPagination(this.getFileSendAcceptService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("viewCaogaoFile")
	public String viewCaogaoFile(){
		return SUCCESS;
	}
	
	@Action("editCaogaoFile")
	public String editCaogaoFile(){
		return SUCCESS;
	}
	
	@Action("listSendFile")
	public String listSendFile(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				FileSend.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("recorder", person));
		criteria.add(Restrictions.eq("oabox", 1));
		this.setPagination(this.getFileSendService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action(value = "removeFileSendAccept", results = { @Result(name = "success", type = "redirect", location = "listReceiveFile") })
	public String removeFileSendAccept() {
		this.getFileSendAcceptService().batchRemove(ids);
		return SUCCESS;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public String getAccepts() {
		return accepts;
	}
	public void setAccepts(String accepts) {
		this.accepts = accepts;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public FileSend getFileSend() {
		return fileSend;
	}

	public void setFileSend(FileSend fileSend) {
		this.fileSend = fileSend;
	}

	public FileSendService getFileSendService() {
		return fileSendService;
	}

	public void setFileSendService(FileSendService fileSendService) {
		this.fileSendService = fileSendService;
	}

	public FileSendAcceptService getFileSendAcceptService() {
		return fileSendAcceptService;
	}

	public void setFileSendAcceptService(FileSendAcceptService fileSendAcceptService) {
		this.fileSendAcceptService = fileSendAcceptService;
	}
	
	public FileSendCollectService getFileSendCollectService(){
		return fileSendCollectService;
	}
	
	public void setFileSendCollectService(FileSendCollectService fileSendCollectService){
		this.fileSendCollectService =  fileSendCollectService;
	}
}
